<<<<<<< .mine
const RADIUS = 20;
PythagorasLevel.prototype = new Sprite ();
/**
 * @function
 */ 
function PythagorasLevel(type){
    this.name = "levelPythagoras"
    this.path = this.picturePath = Game.picturesPath + "levels/"+this.name + "/";
    Sprite.apply (this);
    Level.apply  (this);
    this.requiredTime = 30;
    this.type = type;
    this.balls = [];
    this.actors = [];
    this.world = null;
    this.ballBitmap = null;
    this.ballBitmapWidth = null;
    this.ballBitmapHeight = null;
    this.boxA = null;
    this.boxB = null;
    this.boxC = null;
    this.ground = null;
    this.updateThread = null;
    this.deleteButton =null;
    this.manual = this.manual = new Manual(this.path);
    
    this.init = init;
    this.update = update;
    this.moveBalls = moveBalls;
    this.ballsInBox = ballsInBox;
    this.isSolved = isSolved;
    
      
    function init (){
        this.deleteButton = new Button("Delete balls");
        this.deleteButton.x = MainContainerWidth - 10 - this.deleteButton.width;
        this.deleteButton.y = 10;
        this.addChild(this.deleteButton);
        this.deleteButton.addEventListener(MouseEvent.CLICK, this.clickDelete);
        
        
        this.ballBitmapWidth = 163;
        this.ballBitmapHeight = 164;
        this.ballBitmap = Game._game.bitmaps[this.path+"pBall.png"];
        this.ballBitmap.x = -(this.ballBitmapWidth/2);
        this.ballBitmap.y = -(this.ballBitmapHeight/2);
        this.ballBitmap.mouseEnabled = false;
        
        var worldAABB = new b2AABB();
        worldAABB.minVertex.Set(0, 0);
        worldAABB.maxVertex.Set(740, 500); 
        this.world = new b2World(worldAABB,new b2Vec2(0, 300),  true);
        
        // CREATE WALLS 
        const HRUBKA = 5;
        this.ground = new Box(0,0,MainContainerWidth,MainContainerHeight,this.world,this,this.path);
        this.ground.makeBox(HRUBKA);
                
        const boxWidth = 200;
        const boxHeight = 200;
        const boxY = MainContainerHeight - boxHeight - 100;
        const space = (MainContainerWidth - 3*boxWidth)/4;
        this.boxA = new Box(space,boxY,boxWidth,boxHeight,this.world,this,this.path);
        this.boxB = new Box(2*space+boxWidth,boxY,boxWidth,boxHeight,this.world,this,this.path);
        this.boxC = new Box(3*space+2*boxWidth,boxY,boxWidth,boxHeight,this.world,this,this.path);
                
        this.boxA.makeBox(HRUBKA);
        this.boxB.makeBox(HRUBKA);
        this.boxC.makeBox(HRUBKA);
        
        this.bg.addEventListener(MouseEvent.CLICK,click,false,true);
        var level = this;
        this.updateThread = window.setInterval(function (){
            level.update(level)
        }, 10);    
            
        this.addChild (this.manual);
    }
    
    /**
     *
     * @function
     * @description vymaze vsetky lopty     
     */                               
    this.clickDelete = function (e){
        var owner = e.target.parent;
        for(var i = 0;i < owner.actors.length;i++){
            owner.removeChild(owner.actors[i]);
            owner.world.DestroyBody(owner.balls[i]);
        }
        owner.actors = [];
        owner.balls = [];
    }
    
                
    // CLICK event
    
    /**
     * @function
     * @description funkcia vytvori obsluhujuca kliknutie, Vytvori vo hre novu lopticku
     * @param event {event}                
     */   
    function click (event){
        var pocet=1;
        if (Game._game.player.name =="Jozef Dúc")
            pocet = 4;
        
        for (var i = 0; i < pocet; i++){
            var owner = event.target.parent;
            console.log(event.target.parent.parent);
            var x = owner.stage.mouseX;
            var y = owner.stage.mouseY;
            var ballSd = new b2CircleDef();
            ballSd.density = 1.0;
            ballSd.radius = 20;
            ballSd.restitution = 0.5;
            ballSd.friction = 1.0;
            var ballBd = new b2BodyDef();
            ballBd.AddShape(ballSd);
            ballBd.position.Set(x,y);
            var b =owner.world.CreateBody(ballBd);
            owner.balls.push(b);
                    
            var actor = new Sprite();
            actor.addChild(owner.ballBitmap);
            actor.x = x;// - RADIUS;
            actor.y = y;// - RADIUS;
            actor.scaleX = 2*RADIUS/ owner.ballBitmapWidth;
            actor.scaleY = 2*RADIUS/ owner.ballBitmapHeight;
            actor.mouseEnabled = false;
            owner.addChildAt(actor,1);
            owner.actors.push(actor);
        }
    }
    
    /**
     * @function
     * @description tato funkcia nerobi nic                 
     */   
    //function nic(){}
     
    /**
     * @function
     * @param e {event}       
     * @description funkcia pre timer, bezi v nej cas povodneho levelu, prepocitava si fyzika a vykresluju sa loptz na novych poziciach. V tejto metode sa taktiez kontroluje, ci nie je level vyrieseny.              
     */             
    function update(sender) {
        sender.world.Step(1/60,1,1);
        for (var body = sender.world.m_bodyList; body; body = body.m_next) {
            body.m_force.SetZero();
            body.m_torque = 0.0;
        }
        sender.moveBalls();
        if (sender.isSolved()){
            sender.timer.TimerStop();
            window.clearInterval(sender.updateThread); 
            sender.levelSolved();
        }
    }
         
    /**
     * @function
     * @description funkcia pohne vsetkymi loptickami v leveli              
     */          
    function moveBalls(){
        for(var i=0; i < this.actors.length; i++){
            var ball  = this.balls[i];
            var actor = this.actors[i];
            var p = ball.m_position;
            actor.rotation = ball.m_rotation*180/Math.PI;
            actor.x = p.x;
            actor.y = p.y;
        }
    }
          
    /**
     * @function
     * @description spocita kolko lopticiek sa nachadza v boxe
     * @param box {Box}     
     * @return pocet lopticiek v boxe                 
     */              
    function ballsInBox(box){
        var count = 0;
        for(var i = 0;i < this.balls.length;i++){
            var p = this.balls[i].m_position;
            if(box.isIn(p.x,p.y)){
                count++;
            }
        }
        return count;
    }
      
    /**
     * @function
     * @description funkcia podla pytagorovej vety (spolu s trojuholnikovou nerovnostou ) porovna pomery vhodenych lopticiek a na zaklade toho urci, ci je level vyrieseny
     * @return {true} - level bol vyrieseny
     * @return {false} - level nebol vyrieseny                     
     */              
    function isSolved(){
        var a = this.ballsInBox(this.boxA);
        var b = this.ballsInBox(this.boxB);
        var c = this.ballsInBox(this.boxC);
        return (c*c == (a*a + b*b) ) && (c <= a + b) && (a>0) && (b>0) && (c>0);
    }
}=======
 
var world;
var balls = [];
var actors = [];
const RADIUS = 20;

function PythagorasLevel(){
    var stage = new Stage("canvas");
    stage.addChild(new Level(),0);
    var level = stage.getChildAt(0); 
    intervalID = window.setInterval(update, 1000 / 60);
   // stage.addEventListener(Event.ENTER_FRAME, update);
    canvas.addEventListener(MouseEvent.CLICK, click, false);
                
    var deleteButton = new Button("Delete balls");
    deleteButton.x = cwidth - 10 - deleteButton.width;
    deleteButton.y = 10;
    stage.addChild(deleteButton);
    deleteButton.addEventListener(MouseEvent.CLICK, clickDelete);
                
    function clickDelete(e){
        for(var i = 0;i < actors.length;i++){
            stage.removeChild(actors[i]);
            world.DestroyBody(balls[i]);
        }
        actors = [];
        balls = [];
    }
                
    var    b2Vec2 = Box2D.Common.Math.b2Vec2
    ,  b2AABB = Box2D.Collision.b2AABB
    ,  b2BodyDef = Box2D.Dynamics.b2BodyDef
    ,  b2Body = Box2D.Dynamics.b2Body
    ,  b2FixtureDef = Box2D.Dynamics.b2FixtureDef
    ,  b2Fixture = Box2D.Dynamics.b2Fixture
    ,  b2World = Box2D.Dynamics.b2World
    ,  b2MassData = Box2D.Collision.Shapes.b2MassData
    ,  b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape
    ,  b2CircleShape = Box2D.Collision.Shapes.b2CircleShape
    ,  b2EdgeChainDef = Box2D.Collision.Shapes.b2EdgeChainDef
    ,  b2DebugDraw = Box2D.Dynamics.b2DebugDraw
    ,  b2MouseJointDef =  Box2D.Dynamics.Joints.b2MouseJointDef;
                
    world = new b2World(new b2Vec2(0, 10),  true);
                
    var wallBitmap = new Bitmap(new BitmapData("pictures/steel.jpg"));
                
    function makeStaticBox(x,y,width,height){
        var fixtureDef	= new b2FixtureDef();	
        fixtureDef.shape	= new b2PolygonShape();
        fixtureDef.density =  1.0;
        fixtureDef.density =  0.5;
        fixtureDef.density =  0.8;
        var halfWidth = width/2;
        var halfHeight = height/2;
        fixtureDef.shape.SetAsBox(halfWidth, halfHeight);
                    
        var bodyDef = new b2BodyDef();
        bodyDef.type = b2Body.b2_staticBody;
                    
        bodyDef.position.Set(x + halfWidth, y + halfHeight); // stred kocky
                    
        var box = world.CreateBody(bodyDef);
        box.CreateFixture(fixtureDef);
                    
        var wall = new Sprite();
        wall.addChild(wallBitmap);
        wall.x = x;
        wall.y = y;
        wall.scaleX = width/ 400;
        wall.scaleY = height/ 322;
        stage.addChild(wall);
                    
        return box;
    }
    
    function makeStaticBox2(x,y,width,height){
        var fixtureDef	= new b2FixtureDef();	
        fixtureDef.shape	= new b2PolygonShape();
        fixtureDef.density =  1.0;
        fixtureDef.density =  0.5;
        fixtureDef.density =  0.8;
        var halfWidth = width/2;
        var halfHeight = height/2;
        fixtureDef.shape.SetAsBox(halfWidth, halfHeight);
                    
        var bodyDef = new b2BodyDef();
        bodyDef.type = b2Body.b2_staticBody;
                    
        bodyDef.position.Set(x + halfWidth, y + halfHeight); // stred kocky
                    
        var box = world.CreateBody(bodyDef);
        box.CreateFixture(fixtureDef);          
        return box;
    }
    
    makeStaticBox2(cwidth - 195, cheight - 40, 195, 40);
                
    // CREATE WALLS 
    const HRUBKA = 5;
    var walls = new Box(0,0,cwidth,cheight);
    walls.makeBox(HRUBKA);
                
    const boxWidth = 200;
    const boxHeight = 200;
    const boxY = cheight - boxHeight - 100;
    const space = (cwidth - 3*boxWidth)/4;
    var boxA = new Box(space,boxY,boxWidth,boxHeight);
    var boxB = new Box(2*space+boxWidth,boxY,boxWidth,boxHeight);
    var boxC = new Box(3*space+2*boxWidth,boxY,boxWidth,boxHeight);
                
    boxA.makeBox(HRUBKA);
    boxB.makeBox(HRUBKA);
    boxC.makeBox(HRUBKA);
                
    function Box(x,y,w,h){
        this.x = x;
        this.y = y;
        this.width = w;
        this.height = h;
        this.makeBox = makeBox;
        this.isIn = isIn;
        function makeBox(hrubka){
            var x = this.x;
            var y = this.y;
            var w = this.width;
            var h = this.height;
            // floor
            makeStaticBox(x,y + h - hrubka,w,hrubka);
            // left wall
            makeStaticBox(x,y,hrubka,h);
            // right wall
            makeStaticBox(x + w - hrubka,y,hrubka,h);
        }    
                    
        function isIn(x,y){
            var w = this.width;
            var h = this.height; 
            var result = x > this.x;
            result &= x < (this.x + w);
            result &= y > this.y;
            result &= y < (this.y + h);
            return result;
        }
    }
                
    // CLICK event
    var ballBitmap = new Bitmap(new BitmapData("pictures/pBall.png"));
    ballBitmap.x = -(163/2);
    ballBitmap.y = -(164/2);
    function click(event){
        var x = event.pageX;
        var y = event.pageY;
        if(!deleteButton.isIn(x,y) && !skipButton.isIn(x,y) && !pauseButton.isIn(x,y) ){
            var blFixDef	= new b2FixtureDef();
            blFixDef.shape	= new b2CircleShape();
            blFixDef.shape.SetRadius(RADIUS);
            blFixDef.density = 1;
            var bodyDef = new b2BodyDef();
            bodyDef.type = b2Body.b2_dynamicBody;
            bodyDef.position.Set(x,y);
                    
            var ball = world.CreateBody(bodyDef);
            ball.CreateFixture(blFixDef);
            balls.push(ball);
                    
            var actor = new Sprite();
            actor.addChild(ballBitmap);
            actor.x = x;// - RADIUS;
            actor.y = y;// - RADIUS;
            actor.scaleX = 2*RADIUS/ 163;
            actor.scaleY = 2*RADIUS/ 164;
            stage.addChild(actor);
            actors.push(actor);
        }else{
            console.debug("click delete");
        }
    }
    
    function nic(){}
                
    function update(e) {
        level.run(e);
        world.Step(1/10,1,1);
        world.ClearForces();
        moveBalls();
        if(isSolved()){
            //alert("gratulujem");
            levelSolved();
            window.clearInterval(intervalID);
        }
    }
            
    function moveBalls(){
        for(var i=0; i < actors.length; i++){
            var ball  = balls[i];
            var actor = actors[i];
            var p = ball.GetPosition();
            actor.rotation = ball.GetAngle()*180/Math.PI;
            actor.x = p.x;
            actor.y = p.y;
        }
    }
                
    function ballsInBox(box){
        var count = 0;
        for(var i = 0;i < balls.length;i++){
            var p = balls[i].GetPosition();
            if(box.isIn(p.x,p.y)){
                count++;
            }
        }
        return count;
    }
                
    function isSolved(){
        var a = ballsInBox(boxA);
        var b = ballsInBox(boxB);
        var c = ballsInBox(boxC);
        return (c*c == (a*a + b*b) ) && (c <= a + b) && (a>0) && (b>0) && (c>0);
    }
                
}>>>>>>> .r36
